package com.x.provider.customer.factory.dto.impl;

import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerStatDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.model.domain.customer.CustomerStat;
import com.x.provider.customer.service.customer.CustomerStatService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerStatBuilder")
public class CustomerStatBuilderImpl implements CustomerDTOBuilder {

   private final CustomerStatService customerStatService;

   public CustomerStatBuilderImpl(CustomerStatService customerStatService){
      this.customerStatService = customerStatService;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER_STAT.name())){
         return;
      }
      Map<Long, CustomerStat> statistics = customerStatService.list(listCustomerAO.getCustomerIds());
      statistics.entrySet().forEach(item -> {
         dest.get(item.getKey()).setStatistic(BeanUtil.prepare(item.getValue(), CustomerStatDTO.class));
      });
   }

}
